//
//  LBLightningViewController.m
//  LvoeBeen_Imitation
//
//  Created by Mac on 17/2/18.
//  Copyright © 2017年 itcast. All rights reserved.
//

#import "LBLightningViewController.h"
#import "LBSuperMarketCategories.h"
#import "LBLeftTableViewController.h"
static CGFloat leftTableViewWidth = 100;

@interface LBLightningViewController ()

@property (strong,nonatomic) NSArray<LBSuperMarketCategories *> *superMarketCategories;

@property (weak,nonatomic) LBLeftTableViewController *leftTableViewController;
@property (weak,nonatomic) LBLeftTableViewController *rightTableViewController;

@end

@implementation LBLightningViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadGoods];
//    [self buildNavigationBarUI:self];
    [SVProgressHUD showWithStatus:@"正在加载"];
    [self buildLightSuperMaketUI];
}

#pragma mark
#pragma mark - 搭建界面
- (void) buildLightSuperMaketUI {
    // 实例化左表格
    LBLeftTableViewController *categoriesTableViewController = [[LBLeftTableViewController alloc] initWithSureIsLeft:YES];
    categoriesTableViewController.categories = self.superMarketCategories;
    self.leftTableViewController = categoriesTableViewController;
    categoriesTableViewController.view.frame = CGRectMake(0, 0, leftTableViewWidth, ScreenHeight);
    // 实例化右表格
    LBLeftTableViewController *goodsTableViewController = [[LBLeftTableViewController alloc] initWithSureIsLeft:NO];
    goodsTableViewController.categories = self.superMarketCategories;
    self.rightTableViewController = goodsTableViewController;
    goodsTableViewController.view.frame = CGRectMake(leftTableViewWidth, 64, ScreenWidth - leftTableViewWidth, ScreenHeight);
    [self addChildViewController:categoriesTableViewController];
    [self addChildViewController:goodsTableViewController];
    [self.view addSubview:categoriesTableViewController.view];
    [self.view addSubview:goodsTableViewController.view];
    
    categoriesTableViewController.delegate = goodsTableViewController;
}

#pragma mark
#pragma mark - 加载闪电超市商品
- (void) loadGoods {
    [LBSuperMarketCategories loadSuperMarketGoodsWithBlock:^(NSArray *categories, NSError *error) {
        if (error) {
            [SVProgressHUD showErrorWithStatus:@"数据加载失败，请检查网络"];
            return ;
        }
        
        self.superMarketCategories = categories;
        self.leftTableViewController.categories = categories;
        self.rightTableViewController.categories = categories;
        [SVProgressHUD showSuccessWithStatus:@"加载完毕"];
        
        // 延迟1.5秒，消失动画
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    }];
}


@end
